Images = {}

-- TOOLBOX CLASS DEF
Toolbox = {
	InfoText = "",
	Bounds = {x=16,y=graphics.ScrH()-16-44*2, w=graphics.ScrW()-32, h= 44*2 + 4},
	Items = {},
	selectedTool = nil,
}
class(Toolbox) -- register toolbox as a class

-- toolbox constructor
function Toolbox:new()
	self.className = "ToolBox"
	-- all of the key names we want to show on the toolbar
	local list = {
		"blockSolid","redCrossOn","redTorchOn","woodTouchpadOff",
		"leverOff","buttonOff","normalPiston","diode1","woodDoor1","woodLatch"
	}
	local x,y = self.Bounds.x+4,self.Bounds.y+4
	for k,v in pairs(list) do
		-- make a new instance of toolitem
		local newitem = ToolItem( ITEMS[v].inv, ITEMS[v].c, v, ITEMS[v] )
		-- add the new toolitem to the list
		self:Add( newitem )
	end
	
	self:PerformLayout()
	table.insert(MainState.Renderables, self)
end

function Toolbox:Add(item)
	self.Items[#self.Items+1] = item
end

function Toolbox:PerformLayout()
	local x,y = self.Bounds.x+4,self.Bounds.y+4
	for k,v in pairs(self.Items) do
		v.Bounds.x,v.Bounds.y = x,y
		if x+44 > self.Bounds.w then y=y+44 x=0 else x=x+44 end
	end
end

function Toolbox:Update()

	-- TODO: comment and refactor this code
	if input.mouseDown(1) then
		if input.mouseMoved() then
			local mx,my = input.mousePos()
			for k,v in pairs(self.Items) do
				local x,y,w,h = v.Bounds.x,v.Bounds.y, v.Bounds.w,v.Bounds.h
				if mx>=x and my>=y and mx<=x+w and my<=y+h then
					self.InfoText = v.ToolTip
					
					print("test")
					if self.selectedTool ~= nil then 
						self.selectedTool.selected = false
					end
					self.selectedTool = v
					self.selectedTool.selected = true
					
					break
				end
			end
		end
	end
end

function Toolbox:Draw()
	-- draw text
	graphics.drawText(self.InfoText, 650,0)
	if self.selectedTool ~= nil then
		graphics.drawText(self.selectedTool.ToolTip, 650,12)
	end

	-- draw the background rect
	local x,y,w,h = self.Bounds.x, self.Bounds.y, self.Bounds.w, self.Bounds.h
	graphics.setColor(200,200,200,200)
	graphics.drawRect(x,y,w,h, true)
	
	--draw all the items
	graphics.setColor(255,255,255,255)
	for k,v in pairs(self.Items) do
		v:Draw()
	end
end




-- TOOL ITEM CLASS DEF
ToolItem = {
	Bounds = {x=0,y=0,w=40,h=40},
	ref = "",
	ID = "",
	Color = {255,255,255,255},
	ToolTip = "",
	selected = false,
}
class(ToolItem)

function ToolItem:new(id, c, tip, ref)
	self.className = "ToolItem"
	self.ID = id
	self.Color = c
	self.ToolTip = tip
	self.ref = ref
end

function ToolItem:Update()
end

function ToolItem:Draw()
	local bounds = self.Bounds
	local src = self.ref.Source

	graphics.setColor(100,100,100,255)
	graphics.drawRect(bounds.x,bounds.y, bounds.w,bounds.h, true)
	
	if self.selected then
		graphics.setColor(0,255,0,255)
		graphics.drawRect(bounds.x,bounds.y, bounds.w,bounds.h, false)
	end
	
	local r,g,b,a = unpack(self.Color)
	graphics.setColor(r,g,b,a)
	
	graphics.drawPart(sheet, 
						bounds.x+4,bounds.y+4, bounds.w-8,bounds.h-8,
						src.x,src.y,src.w,src.h)
end
